<template>
  <div>
    <div class="container"
         ref="imageDom">
      <van-nav-bar :title="$t('title')"
                   left-arrow
                   @click-left="$router.back()" />
      <div class="tab">
        <ul>
          <li :ref="'tabs'+item.id"
              v-for="item in tab"
              :key="item.id"
              @click="activeName = item.id,finished = false"
              :class="item.id ===activeName?'current':''">
            {{item.label}}
          </li>
        </ul>
      </div>
      <div class="list">
        <van-list v-model="loading"
                  :finished="finished"
                  @load="onLoad">
          <ul>
            <li v-for="(item,index) in rankingList"
                :key="index"
                :class="index < 3?'topThree':''">
              <div class="icon">
                <span>{{index+1}}</span>
              </div>
              <div class="left">
                <van-image :src="item.avatar || userInfo.avatar" />
                <div class="name">{{item.user_nickname || item.mobile}}</div>
                <!-- <div class="yearRate">
                  <div class="label">年化收益率</div>
                  <div class="rate">+223.45%</div>
                </div> -->
              </div>
              <div class="right">
                <div class="dayProfit">{{$t('todayReward')}}：{{item.today_reward}}</div>
                <div class="total">
                  {{$t('allReward')}}：{{item.all_reward}}
                </div>
              </div>
            </li>
          </ul>
        </van-list>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState, mapActions } from 'vuex'
export default {
  i18n: {
    messages: {
      zh: {
        todayRanking: '今日排行',
        totalRanking: '总榜单',
        title: "排行榜",
        todayReward: "今日收益",
        allReward: "总收益"
      },
      en: {
        todayRanking: `Today's ranking`,
        totalRanking: 'Total list',
        title: "Leaderboard",
        todayReward: "Today's earnings",
        allReward: "Total revenue"
      },
      jp: {
        todayRanking: '今日のランキング',
        totalRanking: 'トータルリスト',
        title: "リーダーボード",
        todayReward: "今日の収益",
        allReward: "総収入"
      },
      kr: {
        todayRanking: '오늘의 순위',
        totalRanking: '총 목록',
        title: "리더보드",
        todayReward: "오늘의 수익",
        allReward: "총 수익"
      },
    }
  },
  data () {
    return {
      tab: [{
        label: this.$t('todayRanking'),
        id: 1
      },
      {
        label: this.$t('totalRanking'),
        id: 2
      }],
      activeName: 1,
      rankingList: [],
      loading: false,
      finished: false,
    }
  },
  computed: {
    ...mapState({
      userInfo: ({ user }) => user.userInfo || {},
    }),
  },
  methods: {
    onLoad () {
      this.rankingList = []
      this.$store.dispatch("ranking/rankingList", { type: this.activeName }).then((res) => {
        console.log(res)
        this.rankingList = res.data.list
        this.loading = false;
        this.finished = true;
      });
    }
  }
}
</script>>
<style lang="less" scoped>
.tab{
    height: 45px;
    background: #FFFFFF;
    border-radius: 8px;
    ul{
      padding: 2px 4px;
      display: flex;
      align-items: center;
      li{
        flex: 1;
        height: 40px;
        line-height: 40px;
        font-size: 18px;
        font-weight: 500;
        color: #9B9B9B;
        text-align: center;
        &.current{
          color: #FFFFFF;
          background: linear-gradient(90deg, #366FF9 0%, #6D97FA 100%);
          border-radius: 8px;
        }
      }
    }
  }
.list{
  margin-top: 10px;
  padding: 0 16px;
  line-height: 1;
  ul{
    li{
      position: relative;
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 12px;
      height: 65px;
      padding: 0 11px 0 33px;
      background: #FFFFFF;
      color: #000000;
      box-shadow: 0px 4px 12px 0px rgba(0, 0, 0, 0.1);
      border-radius: 8px;
      &.topThree{
        color: #ffffff;
        background: linear-gradient(142deg, #69AAFD 0%, #3871F9 100%);
        .icon{
          &:after{
            border-top: 39px solid #F5A623FF;
          }
        }
        .left{
          
          .yearRate{
            .rate{
              color: #ffffff;
            }
          }
        }
      }
      .icon{
        position: absolute;
        top:-5px;
        left:-5px;
        height: 40px;
        line-height: 40px;
        width: 51px;
        border-radius: 5px;
        overflow: hidden;
        span{
          position: absolute;
          left: 13px;
          font-size: 18px;
          font-weight: 600;
          color: #FFFFFF;
          z-index: 1;
        }
        &:after{
          content: "";
          position: absolute;
          left: 0;
          top: 0;
          width: 51px;
          border-top: 39px solid #9DB3C0;
          border-right: 25px solid transparent;
        }
      }
      .left{
        display: flex;
        align-items: center;
        .van-image{
          margin-right: 5px;
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        .name{
          font-size: 18px;
          font-weight: 500;
          overflow:hidden;
          text-overflow:ellipsis;
          white-space:nowrap;
          max-width: 100px;
          span{
            font-size: 14px;
          }
        }
        .yearRate{
          display: flex;
          align-items: center;
          margin-top: 6px;
          .label{
            font-size: 11px;
            font-weight: 300;
          }
          .rate{
            font-size: 14px;
            font-weight: 400;
            color: #FF5D5D;
          }
        }
      }
      .right{
        text-align: right;
        .dayProfit{
          font-size: 11px;
          font-weight: 400;
        }
        .total{
          margin-top: 9px;
          font-size: 11px;
          font-weight: 300;
          span{
            margin: 0 5px;
            font-size: 16px;
          }
        }
      }
    }
  }
}
</style>